/**
 * @author trex279
 */

var commentsfileName = "comments.json";
var newComments = {};
var oldComments = {};
var oldCommentsArray = new Array();
var newCommentsArray = new Array();
var now = new Date(); //time now

function getComments(){
	$.getJSON("http://www.reddit.com/user/"+userName_g+"/comments/.json",
    	function(redditJson){
			if (redditJson == "") {
				thisApplication.nativeWin.dispatchEvent(loadingErrorEvent);
				return;
			}
			
			newComments = redditJson;
			newCommentsArray = redditJson.data.children;
			
			var commentsJsonFromFile = readJsonFromFile(commentsfileName);
			if(commentsJsonFromFile==-1){
				//alert("File size 0");
				storeJson(redditJson,commentsfileName);
				commentsJsonFromFile = readJsonFromFile(commentsfileName);
			}
			 
			var oldCommentsJson = $.evalJSON(commentsJsonFromFile);
			oldCommentsArray = oldCommentsJson.data.children;
			
			processComments();
			displayComments();
			storeJson(redditJson, commentsfileName);
		}
	)
}

function displayComments(){
	var sortedArray = new Array();
	var commentsHtml = "";
	commentsHtml+=""+
					"<thead> "+
					"<tr> "+
					    "<th class='sort-alpha votes'>V</th> "+
						"<th class='sort-alpha upvotes'>U</th> "+
						"<th class='sort-alpha downvotes'>D</th> "+
						"<th class='sort-alpha newUpvotes'>NU</th> "+
						"<th class='sort-alpha newDownvotes'>ND</th> "+
					    "<th>Comment</th> "+
					"</tr> "+
					"</thead> "+
					"<tbody> ";
	for(var i = 0; i<newCommentsArray.length; i++){
		if(!newCommentsArray[i].data.newups)
			newCommentsArray[i].data.newups = 0;
		if(!newCommentsArray[i].data.newdowns)
			newCommentsArray[i].data.newdowns = 0;
		commentsHtml+=""+
					"<tr> "+
					    "<td>"+(newCommentsArray[i].data.ups-newCommentsArray[i].data.downs)+"</td>"+
						"<td>"+newCommentsArray[i].data.ups+"</td>"+
						"<td>"+newCommentsArray[i].data.downs+"</td> "+
					    "<td class='newUpvotes'>"+newCommentsArray[i].data.newups+"</td>"+
						"<td class='newDownvotes'>"+newCommentsArray[i].data.newdowns+"</td> "+
					    "<td class='text'><span class='subject'>"+newCommentsArray[i].data.link_title+"</span><span class='commentBody'>"+toHtml(newCommentsArray[i].data.body_html)+"</span></td> "+  
					"</tr>";

	}
	commentsHtml+="</tbody>";
	$('#theComments table').html(commentsHtml);
	sortables_init();
	
	//tooltips for table heads
	$('#commentSortTab table.sortable th.votes').simpletip({
		content: 'Total Votes',
		fixed: true,
		position: ["0", "-30"]
	});
	$('#commentSortTab table.sortable th.upvotes').simpletip({
		content: 'Up Votes',
		fixed: true,
		position: ["0", "-30"]
	});
	$('#commentSortTab table.sortable th.downvotes').simpletip({
		content: 'Down Votes',
		fixed: true,
		position: ["0", "-30"]
	});
	$('#commentSortTab table.sortable th.newUpvotes').simpletip({
		content: 'New Up Votes',
		fixed: true,
		position: ["0", "-30"]
	});
	$('#commentSortTab table.sortable th.newDownvotes').simpletip({
		content: 'New Down Votes',
		fixed: true,
		position: ["0", "-30"]
	});
	
	thisApplication.nativeWin.dispatchEvent(loadingCompleteEvent);
}

function processComments(){
	var i = 0;
	var j = 0;
	newComments.at = now.getTime();
	while (!(i == oldCommentsArray.length || j == newCommentsArray.length)) {
		if (oldCommentsArray[i].data.id == newCommentsArray[j].data.id) {
			newCommentsArray[j].data.newups = (newCommentsArray[j].data.ups - oldCommentsArray[i].data.ups);

			newCommentsArray[j].data.newdowns = (newCommentsArray[j].data.downs - oldCommentsArray[i].data.downs);

			i++;
			j++;
		}
		else 
			if (compareCommentIds(oldCommentsArray[i].data.id, newCommentsArray[j].data.id) == oldCommentsArray[i].data.id) {
				//when the user has deleted some comments. So nothing to do here
				i++;
			}
			else {//when new commentid greater than old comment id (i.e. when the user has made new comments)
				newCommentsArray[j].data.newups = (newCommentsArray[j].data.ups);
				newCommentsArray[j].data.newdowns = (newCommentsArray[j].data.downs);
				j++;
			}
	}
}

function compareCommentIds(commentId1, commentId2){
	//returns the commentId that is the greatest
	length1 = commentId1.length;
	length2 = commentId2.length;
	if(commentId1.charAt(length1-1)>commentId2.charAt(length2-1)){
		return commentId1;
	}
	else
		if(commentId1.charAt(length1-1)<commentId2.charAt(length2-1)){
			return commentId2;
		}
		else{
			var biggerCommentId = compareCommentIds(commentId1.substr(0,length1-1), commentId2.substr(0,length2-1));
			if(biggerCommentId==-1) alert("Error");
			if(commentId1.match(biggerCommentId)){
				return commentId1
			}
			else if(commentId2.match(biggerCommentId)){
				return commentId2;
			}
			else return -1;
		}
}